﻿///<reference path="_moduleReferences.ts" />

module ScrambledEggs.Modules {
    export class Birth implements IModule {

        private _onUpdate: () => void;

        constructor() {
        }

        initialize(gameState: GameStateProvider, onUpdate: () => void ) {
            this._onUpdate = onUpdate;
        }

        update(gameState: GameStateProvider, triggerSource: IModule): void {
            var me = this;
            var state = gameState.State;

            if (state.Gender != Gender.Unknown || state.Phase > GamePhase.Fetus) {
                $("html").addClass("born");
                $("#world").hide();
                $("#heartbeat").hide();
                $("#genderSelection").remove();
                return;
            }
            $("html").removeClass("born");
            $("#heartbeat").show();

            var setBg = function (color, callback) { $("html").animate({ backgroundColor: color }, 1300, callback); }            
            setBg("#220000", function () {
                setBg("#110000", function () {
                    setBg("#150000", function () {
                        setBg("#110000", function () {
                            setBg("#220000", function () {
                                setBg("#110000", function () {
                                    setBg("#150000", function () {
                                        setBg("#442222", function () {
                                            setBg("#663333", function () {
                                                setBg("#333366", function () {
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
            this.heartbeatLoop(10);
            var heartbeat = $("#heartbeat");
            var heartbeatCloneInterval = setInterval(function () {
                heartbeat.clone().removeAttr("id").insertAfter(heartbeat).fadeOut(2000, function () { $(this).remove(); });
            }, 0);
            setTimeout(function () { clearInterval(heartbeatCloneInterval); }, 15000);

            var windowHeight = $(window).innerHeight() - 20;
            var bodyWidth = $("body").innerWidth();
            setTimeout(function () {
                console.warn("push!");
                var world = $("#world").css({ marginTop: windowHeight / 2, minHeight: 0 }).show();
                world.animate({ minHeight: windowHeight / 7, minWidth: windowHeight / 20, marginTop: windowHeight * 7 / 8 / 2 }, 3000, "linear", function() {
                    world.animate({ minHeight: windowHeight, minWidth: bodyWidth, marginTop: 0 }, 9000, "easeInCubic", function() {
                        $("html").addClass("born");
                        $("#heartbeat").hide();
                        $("#world").hide();
                        $("#genderSelection").show();
                    });

                });
            }, 0);

            $("#genderSelection button").click(function () {
                var clickedButton = this;
                $("#genderSelection").fadeOut("slow", function () {
                    state.Gender = $(clickedButton).is("#gender-male") ? Gender.Male : Gender.Female;
                    $("#nameSelection").submit(function () {
                        $(this).fadeOut("slow", function () {
                            state.Name = $("#name").val();
                            state.Phase = GamePhase.Baby;
                            me._onUpdate();
                        });
                        return false;
                    }).fadeIn("slow", function () { $("#name").focus(); });
                });
            });
        }

        heartbeatLoop(loopCount: number) {
            if (loopCount <= 0)
                return;
            var me = this;
            var halfWindowHeight = $(window).innerHeight() / 4 * 3;
            var halfWindowWidth = $(window).innerWidth() / 2;
            $("#heartbeat").css({ top: halfWindowHeight + "px", left: "0px", zIndex: 10, position: "absolute" });
            me.showFlatLine(0, halfWindowWidth - halfWindowWidth / 8, function () {
                me.showHeartBeat(halfWindowHeight, halfWindowWidth - halfWindowWidth / 8, halfWindowWidth + halfWindowWidth / 8, function () {
                    me.showFlatLine(halfWindowWidth + halfWindowWidth / 8, halfWindowWidth * 2 - 20, function () {
                        me.heartbeatLoop(loopCount - 1)
                    })
                })
            });
        }

        showFlatLine(beginHorizontalPosition: number, endHorizontalPosition: number, callback: () => void): void {
            $("#heartbeat").animate({ left: endHorizontalPosition + "px" }, 1000, "linear", callback);
        }

        showHeartBeat(verticalHeight: number, beginHorizontalPosition: number, endHorizontalPosition: number, callback: () => void ): void {
            var positionStep = (endHorizontalPosition - beginHorizontalPosition) / 8;
            $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep) + "px", top: (verticalHeight + verticalHeight / 20) + "px" }, 125, "linear", function () {
                $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 2) + "px", top: verticalHeight + "px" }, 125, "linear", function () {
                    $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 3) + "px" }, 125, "linear", function () {
                        $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 4) + "px", top: (verticalHeight + verticalHeight / 50) + "px" }, 125, "linear", function () {
                            $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 5) + "px", top: (verticalHeight - verticalHeight / 5) + "px" }, 125, "linear", function () {
                                $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 6) + "px", top: (verticalHeight + verticalHeight / 5) + "px" }, 125, "linear", function () {
                                    $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 7) + "px", top: verticalHeight + "px" }, 125, "linear", function () {
                                        $("#heartbeat").animate({ left: (beginHorizontalPosition + positionStep * 8) + "px" }, 125, "linear", callback)
                                    })
                                })
                            })
                        })
                    })
                })
            });

        }
    }
}